﻿/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.BusinessRules;
using Votations.NSurvey.WebAdmin.UserControls;

namespace Votations.NSurvey.WebAdmin
{
    /// <summary>
    /// Display the survey statistics
    /// </summary>
    public partial class GlobalStats : PageBase
    {
       

        protected void Page_Load(object sender, System.EventArgs e)
        {

            SetupSecurity();
            LocalizePage();

            if (!Page.IsPostBack)
            {
                SurveyList.SurveyId = SurveyId;
                SurveyList.BindDropDown();
                //Header.SurveyId = SurveyId;
                FillFields();
            }

            ResetVotesButton.Attributes.Add("onClick",
                "javascript:if(confirm('" + GetPageResource("ResetVotesConfirmationMessage") + "')== false) return false;");

        }

        private void SetupSecurity()
        {
            CheckRight(NSurveyRights.AccessSecuritySettings, true);
            ResetVotesButton.Visible = CheckRight(NSurveyRights.ResetVotes, false);
            DeleteSaveProgressLinkButton.Visible = CheckRight(NSurveyRights.DeleteUnvalidateEntries, false);
        }

        private void LocalizePage()
        {
            SurveyStatisticsTitle.Text = GetPageResource("SurveyStatisticsTitle");
            SurveyCreationDateLabel.Text = GetPageResource("SurveyCreationDateLabel");
            LastSurveyEntryLabel.Text = GetPageResource("LastSurveyEntryLabel");
            SurveyDisplayTimesLabel.Text = GetPageResource("SurveyDisplayTimesLabel");
            SurveyResultsDisplayTimesLabel.Text = GetPageResource("SurveyResultsDisplayTimesLabel");
            SurveyNumberOfVotesLabel.Text = GetPageResource("SurveyNumberOfVotesLabel");
            SurveyMonthlyStatsLabel.Text = GetPageResource("SurveyMonthlyStatsLabel");
            ResetVotesButton.Text = GetPageResource("ResetVotesButton");
            SaveProgressEntriesLabel.Text = GetPageResource("SaveProgressEntriesLabel");
            DeleteSaveProgressLinkButton.Text = GetPageResource("DeleteSaveProgressLinkButton");
        }


        /// <summary>
        /// Get the current DB stats and fill 
        /// the label with them
        /// </summary>
        private void FillFields()
        {
            // Retrieve the survey data
            SurveyData surveyData =
                new Surveys().GetSurveyById(SurveyId);

            // Assigns the retrieved data to the correct labels
            CreationDateLabel.Text = surveyData.Surveys[0].CreationDate.ToString();
            LastEntryDateLabel.Text =
                surveyData.Surveys[0].IsLastEntryDateNull() ? GetPageResource("NoEntriesRecordedInfo") : surveyData.Surveys[0].LastEntryDate.ToString();

            DisplayTimesLabel.Text = surveyData.Surveys[0].SurveyDisplayTimes.ToString();
            ResultsDisplayTimesLabel.Text = surveyData.Surveys[0].ResultsDisplayTimes.ToString();
            NumberOfVotersLabel.Text = surveyData.Surveys[0].VoterNumber.ToString();
            NumberSaveProgressEntriesLabel.Text = new Voters().GetUnvalidatedVotersCount(SurveyId).ToString();
        }


 

        protected void ResetVotesButton_Click(object sender, System.EventArgs e)
        {
            new Voter().DeleteVoters(SurveyId);
            FillFields();
        }

        private void StatsCalendar_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
        {
            if (_voterStats == null)
            {
                if (!Page.IsPostBack)
                {
                    _voterStats = new Voters().GetMonthlyStats(SurveyId, DateTime.Now.Month, DateTime.Now.Year);
                }
                else
                {
                    _voterStats = new Voters().GetMonthlyStats(SurveyId, StatsCalendar.VisibleDate.Month, StatsCalendar.VisibleDate.Year);
                }
            }

            DataRow[] voterNumberRow = _voterStats.VoterStats.Select("VotesDate = '#" + e.Day.Date.ToShortDateString() + "#'");

            if (voterNumberRow.Length > 0)
            {
                e.Cell.Controls.Add(new LiteralControl(string.Format("<br><b>" + GetPageResource("EntriesNumber") + "</b>", ((VoterStatisticsData.VoterStatsRow)voterNumberRow[0]).VoterCount)));
            }
            else
            {
            }
        }

        protected void DeleteSaveProgressLinkButton_Click(object sender, System.EventArgs e)
        {
            new Voter().DeleteUnvalidatedVoters(SurveyId);
            FillFields();
        }

        VoterStatisticsData _voterStats;
    }

}
